package com.ruifu.pls.repository;


import com.ruifu.pls.entity.DocAsnHeader;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import javax.transaction.Transactional;

public interface GAsnhederRepository
        extends PagingAndSortingRepository<DocAsnHeader, String>,JpaSpecificationExecutor<DocAsnHeader>
{
//正向入库确认（只查99状态）
    @Query
        (value = "    select * from ( select * from doc_asn_header asn  where asn.h_edi_08  is null and asn.edisendflag = 'N' AND asnstatus=99  AND (warehouseid='JD_TEST' or warehouseid='NSLDS') and asnreference1 is not null and customerid is not null order by asn.asncreationtime desc ) where ROWNUM = 1 ",nativeQuery = true)
    public  DocAsnHeader findAsnForAddConform();

    @Query
            (value = "    select * from ( select * from doc_asn_header asn  where asn.h_edi_08  is null and asn.edisendflag = 'N' AND asnstatus=99 AND warehouseid='JD_TEST' and asnreference1 is not null and customerid is not null and asnType = 'THRK' order by asn.asncreationtime desc ) where ROWNUM = 1 ",nativeQuery = true)
    public  DocAsnHeader findAsnForReturnConform();

    //逆向入库新增数据查询
    @Query
        (value = "    select * from ( select * from doc_asn_header asn  where asn.h_edi_08 is null and warehouseid='NSLDS' AND asnstatus=00  and asn.edisendflag = 'N'  and asn.edisendflag != 'J' and asnreference1 is not null and customerid is not null  order by asn.asncreationtime desc ) where ROWNUM = 1 ",nativeQuery = true)
public  DocAsnHeader findAsnForReverseAdd();


    //退入库逆向新增
    @Query
            (value = "    select * from ( select * from doc_asn_header asn  where asn.h_edi_08  is null and asn.edisendflag = 'N'  and asn.edisendflag != 'J'  AND asnstatus=00 AND warehouseid='JD_TEST' and asnreference1 is not null and customerid is not null and asnType = 'THRK' order by asn.asncreationtime desc ) where ROWNUM = 1 ",nativeQuery = true)
    public  DocAsnHeader findAsnForReturnConformAdd();



    //逆向退货新增数据查询
    @Query
            (value = "    select * from ( select * from doc_asn_header asn  where asn.h_edi_08  is null and asn.edisendflag = 'N' AND warehouseid='NSLDS' and asnreference1 is not null and customerid is not null and asnType = 'THRK' order by asn.asncreationtime desc ) where ROWNUM = 1 ",nativeQuery = true)
    public  DocAsnHeader findAsnForReverseReturnAdd();
    @Query(value = "    select * from ( select * from doc_asn_header asn  where asn.h_edi_08  is null and asn.edisendflag = 'N'  order by asn.asncreationtime desc ) where ROWNUM = 1 ",nativeQuery = true)
    public  DocAsnHeader findAsn();

    //订单状态更新接口
    @Query(value = "select * from ( select * from doc_asn_header asn  where asn.h_edi_08  is null and asn.edisendflag = 'N' AND warehouseid='JD_TEST' and asnreference1 is not null and customerid is not null order by asn.asncreationtime desc ) where ROWNUM = 1 ",nativeQuery = true)
    public DocAsnHeader fingByStatus();


    //入库确认接口状态更新
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "UPDATE  doc_asn_header SET edisendflag = 'Y' WHERE   asnno =?1 ",nativeQuery = true)
    public  void updateAsnStatus(String asnno);
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "UPDATE  doc_asn_header SET edisendflag = 'E' WHERE   asnno =?1 ",nativeQuery = true)
    public  void updateAsnStatusEorro(String asnno);
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "UPDATE  doc_asn_header SET edisendflag = 'J' WHERE   asnno =?1 ",nativeQuery = true)
    public  void updateAsnStatusBy(String asnno);
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "UPDATE  doc_asn_header SET edisendflag = 'E' WHERE   asnno =?1 ",nativeQuery = true)
    public  void updateAsnStatusByEE(String asnno);

}
